//
//  HeaderViews.m
//  KCFurProject
//
//  Created by ios程序员 on 15/11/3.
//  Copyright (c) 2015年 ios程序员. All rights reserved.
//

#import "HeaderViews.h"

@implementation HeaderViews
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //添加scrollerView
        self.userInteractionEnabled = YES;
        self.scrollViews = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        [self addSubview:self.scrollViews];
        self.image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, W, 200*HEIGH)];
        [self.image1 setImage:[UIImage imageNamed:@"5"]];
        [self.scrollViews addSubview:self.image1];
        
        self.image2 = [[UIImageView alloc] initWithFrame:CGRectMake(W, 0, W, 200*HEIGH)];
        [self.image2 setImage:[UIImage imageNamed:@"4"]];
        [self.scrollViews addSubview:self.image2];

        self.image3 = [[UIImageView alloc] initWithFrame:CGRectMake(2*W, 0, W, 200*HEIGH)];
        [self.image3 setImage:[UIImage imageNamed:@"3"]];
        [self.scrollViews addSubview:self.image3];
        /* 设置过渡类型 */


        self.scrollViews.contentSize = CGSizeMake(W*4, 0);
        self.scrollViews.showsHorizontalScrollIndicator = YES;
        self.scrollViews.userInteractionEnabled = YES;
        self.scrollViews.pagingEnabled = YES;
        
        //添加pageControl
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(W * 300 / 375, H * 160 / 667, W * 60 / 375, H * 20 / 667)];
//        self.page.backgroundColor = [UIColor clearColor];
        self.page.pageIndicatorTintColor = [UIColor blackColor];
//        self.page.pageIndicatorTintColor = [UIColor clearColor];
//        self.page.pageIndicatorTintColor = [UIColor redColor];
        self.page.currentPageIndicatorTintColor = [UIColor redColor];
//        self.page.currentPageIndicatorTintColor = [UIColor clearColor];
        self.page.numberOfPages = 3;
        [self addSubview:self.page];
        
        self.imageArr = [NSMutableArray array];
        [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(rotain) userInfo:nil repeats:YES];
    }
    return self;
    
}

//轮播效果
- (void)rotain
{
    self.page.currentPage += 1;
    [self.scrollViews setContentOffset:CGPointMake(self.scrollViews.contentOffset.x + W, 0 ) animated:YES];
    if (self.scrollViews.contentOffset.x == (3 - 1 ) * W ) {
        self.page.currentPage = 0;
        [self.scrollViews setContentOffset:CGPointMake(0 , 0) animated:NO];
        
    }
    /* 效果:pageCurl pageUnCurl  kCATransitionFade  cameraIrisHollowOpen */
    CATransition *transtion = [CATransition animation];
    transtion.type = @"rippleEffect";
    transtion.autoreverses = YES;
    transtion.duration = 3;
    transtion.repeatCount = 5;
    /* layer层添加动画 */
    [self.scrollViews.layer addAnimation:transtion forKey:@"cc"];

}


//pageControl随着scrollerView动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//    if (self.scrollViews.contentOffset.x==0) {
//        [self.scrollViews setContentOffset:CGPointMake(self.frame.size.width*2,0) animated:NO];
//    }
//    if (self.scrollViews.contentOffset.x == self.frame.size.width*3) {
//        [self.scrollViews setContentOffset:CGPointMake(self.frame.size.width,0) animated:NO];
//    }

    
    int x = self.scrollViews.contentOffset.x / self.scrollViews.frame.size.width;
    self.page.currentPage = x;
    
}



@end
